# 贴吧搜索获取指定页数据
# https://tieba.baidu.com/f?kw=%E6%88%90%E9%BE%99&pn=0

import requests

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36",
    "Cookie": "BIDUPSID=7c0e881398940863b9db5e9b89ffb894; PSTM=1592276581; H_PS_PSSID=1424_31326_21092_31254_31765_32046_30824_26350_22160; BAIDUID=7c0e881398940863b9db5e9b89ffb894:FG=1; BD_UPN=12314753; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; delPer=0; BD_CK_SAM=1; PSINO=1; COOKIE_SESSION=20_0_0_2_0_2_0_0_0_2_0_0_0_0_0_0_0_0_1592281095%7C3%230_0_1592281095%7C1; H_PS_645EC=e100RR4O5qf00cQAILH8V9XnWSk1Uu%2FW5FHSVQSEhqRu7m7mIFuLii92mjA; BD_HOME=1"
}

page = int(input("请输入指定获取的页数(int类型):"))
pn = (page-1)*50
# 方式一:利用params参数发送带参数的请求
# params = {'pn': 'pn'}
# url = "https://tieba.baidu.com/f?kw=%E6%88%90%E9%BE%99&pn=0"
# response = requests.get(url, headers=headers, params=params)

# 方式二:直接发送带参数的url的请求
url = f"https://tieba.baidu.com/f?kw=%E6%88%90%E9%BE%99&pn={pn}"
response = requests.get(url, headers=headers)


with open(f'tieba第{page}页.html', 'w', encoding='utf-8') as f:
    f.write(response.content.decode())
